import datetime
import logging
from student.models import StudentInvite
from teacher.models import TeacherInvite
from visitor.models import VisitorApply

logger = logging.getLogger("log")


# 过滤教师邀请过期访问
def removeOldTeacherInvite():
    logger.info("===开始===清除过期教职工邀约===")
    teacher_invite_list = TeacherInvite.objects.exclude(status=8)
    for teacher_invite in teacher_invite_list:
        when_time = datetime.datetime.strptime(teacher_invite.when, "%Y-%m-%d %H:%M:%S")
        now = datetime.datetime.now()
        today = now.replace(hour=0, minute=0, second=0, microsecond=0)
        when_time = teacher_invite.when.replace(hour=0, minute=0, second=0, microsecond=0)
        duration = today-when_time
        if duration.days>0:
            teacher_invite.status = 9
            teacher_invite.save()
    logger.info("===结束===清除过期教职工邀约===")


# 过滤访客申请过期访问
def removeOldVisitorApply():
    logger.info("===开始===过滤访客申请过期访问===")
    visitor_apply_list = VisitorApply.objects.exclude(status=8)
    for visitor_apply in visitor_apply_list:
        now = datetime.datetime.now()
        today = now.replace(hour=0, minute=0, second=0, microsecond=0)
        when_time = visitor_apply.when.replace(hour=0, minute=0, second=0, microsecond=0)
        duration = today-when_time
        if duration.days>0:
            visitor_apply.status = 9
            visitor_apply.save()
    logger.info("===结束===过滤访客申请过期访问===")


# 过滤学生邀请过期访问
def removeOldStudentInvite():
    logger.info("===开始===过滤学生邀请过期访问===")

    logger.info("================开始清除过期教职工邀约===================")
    student_invite_list = StudentInvite.objects.exclude(status=8)
    for student_invite in student_invite_list:
        now = datetime.datetime.now()
        today = now.replace(hour=0, minute=0, second=0, microsecond=0)
        when_time = student_invite.when.replace(hour=0, minute=0, second=0, microsecond=0)
        duration = today-when_time
        if duration.days>0:
            student_invite.status = 9
            student_invite.save()
    logger.info("===结束===过滤学生邀请过期访问===")
